<?php
require '../model/Db.php';
require '../model/Page.php';
$Db = Db::getInstance();
$admin_data = $Db->isadmin();
if(!$admin_data){
	$user_data = $Db->islogin();
	if($user_data['job'] == 3){
		$admin_data = $user_data;
	}
}
if(($admin_data || $user_data['job'] != 0) && is_numeric($_GET['type'])){

	$is_deal = $_GET['type'] == 0 ? 0:1;
	$count =  $Db->count([
		table => [gzwx=>'a',content=>'b'],
		where => "a.cid=b.id and a.is_deal={$is_deal} and b.delete_time is NULL"
	]);
	$page = page($count,50,5);
	$cont_data = $Db->find([
		table => [gzwx=>'a',content=>'b'],
		field => 'a.*,b.time',
		where => "a.cid=b.id and a.is_deal={$is_deal} and b.delete_time is NULL order by b.time desc {$page['limit']}"
	]);
	$cont_data = $Db->tohtml($cont_data);
	$mid = $Db->find([
		table => [module=>'a',content => 'b'],
		field => 'a.id',
		where => "a.id=b.mid and b.id={$cont_data[0]['cid']}"
	])[0]['id'];
	//获取管理员id
	$man_json = $Db->find([
		table => ['module'],
		field => 'manager',
		where => "id={$mid}"
	])[0]['manager'];
	$man_ids = json_decode($man_json);
	$index = array_search($user_data['id'], $man_ids->uids);
	$Db->close();
	foreach($cont_data as $item){
	    $td = '';
	    if(!$is_deal && is_numeric($index)){
	        $td = <<<td
	        <td><a target="_blank" href="/nsbbs/details.php?cid={$item['cid']}">查看</a> <span class="btn" id="{$item['cid']}">受理</span> <span class="btn" id="{$item['cid']}">拒绝</span></td>
td;
	    }
		$html .= <<<tr
				<tr>
					<td>{$item['address']}</td>
					<td>{$item['miaoshu']}</td>
					<td>{$item['sender']}</td>
					<td>{$item['sendertell']}</td>
					<td>{$item['time']}</td>
					{$td}
				</tr>
tr;

	}

	if(!$html){
		$html = '空空如也';
	}
	echo json_encode([html=>$html,page => $page['html']]);
}else{
	http_response_code(404);
	echo '<h1 style="color:#337ab7">千呼万唤始出来,犹抱琵琶半遮面</h1>';
}